<?php

class RoleHelper extends Helper {

    private $_rolefield;
    private $_packfield;
    private $pack_rule;
    private $role_rule;

    public function __construct() {
        $this->_rolefield = array('r_id', 'r_code', 'r_name', 'r_package', 'r_remarks', 'r_role');
        $this->_packfield = array('p_id', 'p_code', 'p_name', 'p_text', 'p_role');
        $this->pack_rule = array(
            'p_code' => array('require', '权限包编号不能为空'),
            'p_name' => array('require', '权限包名称不能为空')
        );
        $this->role_rule = array(
            'r_code' => array('require', '角色编码不能为空'),
            'r_name' => array('require', '角色名称不能为空')
        );
        
    }

    /**
     * 获取角色权限
     * @param type $role_id
     * @return type
     */
    public function getId($role_id = 0) {
        Cache::Set('role', 'detail', 1800);
        if (!$role_cache = Cache::Get('id_' . $role_id)) {
            $where = array();
            $where['`r_id` = ?'] = $role_id;
            $roleModel = $this->LoadModel('Public');
            $role_row = $roleModel->getOneRow('role', $this->_rolefield, $where);
            if (!empty($role_row['r_role'])) {
                $role_row['role'] = unserialize($role_row['r_role']);
            }
            Cache::Save('id_' . $role_id, $role);
        } else {
            $role_cache = $role_cache;
        }
        return $role_cache;
    }
    /**
     * 角色列表
     * @param type $where
     * @param type $page
     * @param type $limit
     * @param type $param
     * @return type
     */
    public function roleList($where, $page = 1, $limit = PAGE_SIZE, $param = array()){
        $roleModel = $this->LoadModel('Public'); 
        $data['count'] = $roleModel->getRowCount('role', $where);
        if(!empty($data['count'])){
            $data['all'] = $roleModel->getList('role', $this->_rolefield, $where, array($page, $limit), NULL, array('r_id DESC'));
            Pagination::SetUrl($Param);
            $data['page'] = Pagination::GetHtml($limit, $page, $data['count']);
        }
        return $data;
    }
    
    public function roleAll($where){
        $model = $this->LoadModel('Public');
        $data = $model->getList('role', $this->_rolefield, $where);
        return $data;
    }
    
    /**
     * 角色保存
     * @param type $data
     * @param type $where
     * @return type
     */
    public function roleSave($data, $where = ''){
        if(is_numeric($where)){
            $where = array('`r_id` = ?' => $where);
        }
        $validator = Validator::checkRule($data, $this->role_rule);
        if(!$validator['pass']){
            return array('success' => false, 'msg' => $validator['msg']);
        }
        $roleModel = $this->LoadModel('Public'); 
        if(is_array($data['r_role'])) $data['r_role'] = unserialize($data['r_role']);
        if(empty($where)){
            $id = $roleModel->save('role', $data);
            if($id){
                return array('success' => true, 'msg' => $id);
            }else{
                return array('success' => false, 'msg' => '保存失败');
            }
        }else{
            $roleModel->update('role', $data, $where);
            return array('success' => true);
        }
    }
    /**
     * 角色信息
     * @return type
     */
    public function roleRow($where){
        if(is_numeric($where)){
            $where = array('`r_id` = ?' => $where);
        }        
        $roleModel = $this->LoadModel('Public'); 
        $data = $roleModel->getOneRow('role', $this->_rolefield, $where);
        if(!empty($data['r_role'])){
            $data['r_role_arr'] = unserialize($data['r_role']);
        }
        return $data;
    }
    /**
     * 角色移除
     * @param type $where
     * @return type
     */
    public function roleRemove($where){
        if(is_numeric($where)){
            $where = array('`r_id` = ?' => $where);
        }
        $where['`r_status` = ?'] = 'ok';
        $roleModel = $this->LoadModel('Public'); 
        $do = $roleModel->update('role', array('r_status' => 'del'), $where);
        return $do;
    }
    
    
    public function packSave($data, $where = ''){
        if(is_numeric($where)){
            $where = array('`p_id` = ?' => $where);
        }
        $validator = Validator::checkRule($data, $this->pack_rule);
        if(!$validator['pass']){
            return array('success' => false, 'msg' => $validator['msg']);
        }
        $packModel = $this->LoadModel('Public'); 
        if(is_array($data['p_role'])) $data['p_role'] = unserialize($data['p_role']);
        if(empty($where)){
            $id = $packModel->save('role_package', $data);
            if($id){
                return array('success' => true, 'msg' => $id);
            }else{
                return array('success' => false, 'msg' => '保存失败');
            }
        }else{
            $packModel->update('role_package', $data, $where);
            return array('success' => true);
        }
    }
    
    public function packRow($where){
        if(is_numeric($where)){
            $where = array('`p_id` = ?' => $where);
        }
        $packModel = $this->LoadModel('Public'); 
        $data = $packModel->getOneRow('role_package', $this->_packfield, $where);
        if(!empty($data['p_role'])){
            $data['p_role_arr'] = unserialize($data['p_role']);
        }
        return $data;
    }
    
    public function packAll($where){
        if(is_numeric($where)){
            $where = array('`p_id` = ?' => $where);
        }
        $packModel = $this->LoadModel('Public');
        $data = $packModel->getList('role_package', $this->_packfield, $where);
        return $data;
    }
    
    public function packRemove($where){
        if(is_numeric($where)){
            $where = array('`p_id` = ?' => $where);
        }
        $packModel = $this->LoadModel('Public'); 
        $do = $packModel->update('role_package', array('p_status' => 'del'), $where);
        return $do;
    }
    //以键值对的形式返回r_id=>r_name
    public function roleName($where){
        $data = $this->roleAll($where);
        foreach($data as $k =>$v){
            $msg[$v['r_id']] = $v['r_name'];
        }
        return $msg;
    }

}